package com.cf.api.db;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.sql.DataSource;

/**
 * 功能描述：数据库事务配置
 *
 * @author lujianping@kuparts.com
 * @version 1.0
 * @date 2017/9/14 17:28
 * @since JDK 1.8
 */
@Configuration
public class TransactionConfig implements TransactionManagementConfigurer {

    @Autowired
    private DataSource dataSource;

    @Autowired
    @Qualifier("otherDataSource")
    private DataSource otherDataSource;

    @Primary
    @DependsOn("dataSource")
    @Bean(name = "transactionManager")
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }

    @DependsOn("otherDataSource")
    @Bean(name = "otherTransactionManager")
    public PlatformTransactionManager otherAnnotationDrivenTransactionManager() {
        return new DataSourceTransactionManager(otherDataSource);
    }

}